package com.yisoo.ubag.product.util;

import com.yisoo.ubag.product.constant.TokenConstant;

import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;

public class TokenMd5Util {
    /**
     * 设备token生成公式
     * 前缀MD5 prefixMD5: consumerid+ () = MidMD5（这个给用户）
     * 可以透露给用户的数据：tokenIndex+level
     *
     * MidMD5 = MD5(PREFIX+consumerid,SALT)
     * TOKEN = MD5(MidMD5+tokenIndex+level 1/2/3 + 背包/存储箱 1/0 )
     */

    public static String getMD5(String str,String SALT){
        String s = DigestUtils.md5DigestAsHex(str.getBytes());
        System.out.println(s);
        return s;
    }

    public static String getMD5ToConsumer(Long consumerId){
        String str = String.valueOf(consumerId);
        str = TokenConstant.PREFIX+str+TokenConstant.SALT;
        String s = DigestUtils.md5DigestAsHex(str.getBytes());
        return s.substring(0,15);
    }
    public static String getTokenALL(Long consumerId,Integer index,String level,Integer tokenUsing){
        String MidMD5 = TokenMd5Util.getMD5ToConsumer(consumerId);

        //level = 1
        //这里用level 100
        return DigestUtils.md5DigestAsHex((MidMD5+index+level+"00"+tokenUsing).getBytes());
    }

    //简单点生成
    public static String getSimpleToken(Long consumerId){
        String time = new Date().toString();
        String uuid = UUID.randomUUID().toString();
        return DigestUtils.md5DigestAsHex((time+uuid+String.valueOf(consumerId)).getBytes());
    }
}
